package com.efast.dmom.mes.datahandlers.entity;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.efast.dmom.mes.bean.QualityCheck;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.dao.entity.QualityCheckDao;
import com.efast.dmom.mes.datahandlers.base.QualityBaseDataHandler;
import com.efast.dmom.mes.entity.QualityCheckEntity;
import com.efast.dmom.mes.util.SpringWebUtils;
import com.efast.dmom.mes.util.TimeUtil;
@Component
public class QualityCheckHandler extends QualityBaseDataHandler<QualityCheck>{

	@Autowired
	private QualityCheckDao qualityCheckDao = null;

	@Override
	public ResultBean save(QualityCheck obj) {
		ResultBean resultBean = new ResultBean();
		obj.getInnerData().setSiteId(DataHandlerUtils.getSiteId());
		obj.getInnerData().setCreationTime(TimeUtil.getLocalDateTime());
		obj.getInnerData().setCreationTimeZone(TimeUtil.getLocalTimeZone());
		obj.getInnerData().setCreator(SpringWebUtils.getRequestUser());
		obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
		obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
		obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
//	    obj.getInnerData().setCreationPid(DataHandlerUtils.getDmomPid());
//	    obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());
		obj.getInnerData().setRowStatus(1);
		obj.getInnerData().setRowTrxId(1);
		qualityCheckDao.save(obj.getInnerData());
		return resultBean;
	}

	@Override
	public ResultBean update(QualityCheck obj) {
		ResultBean resultBean = new ResultBean();
		obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
		obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
		obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
//	    obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());
		obj.getInnerData().setRowTrxId(1);
		qualityCheckDao.update(obj.getInnerData());
		return resultBean;
	}

	@Override
	public ResultBean delete(QualityCheck obj) {
		ResultBean resultBean = new ResultBean();
		return resultBean;
	}

	@Override
	public QualityCheck getById(long id) {
		
		return null;
	}

	@Override
	public QualityCheck getByName(String name) {
		QualityCheck qualityCheck = new QualityCheck();
		qualityCheck.setInnerData(qualityCheckDao.getByName(name));
		return qualityCheck;
	}

	@Override
	public void batchDeleteById(List<QualityCheck> list) {
		List<QualityCheckEntity> objs = new ArrayList<QualityCheckEntity>();
		for (QualityCheck obj : list) {
			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
//		    obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());
			obj.getInnerData().setRowTrxId(1);
			objs.add(obj.getInnerData());
		}
		qualityCheckDao.batchDeleteByIds(objs);
		
	}

	@Override
	public QualityCheck getByCode(String code) {
		
		return null;
	}

	// 通过父id和检测项编码查询数据
	public List<QualityCheck> getByparentidAndCode(QualityCheck qualityCheck) {
		List<QualityCheck> list = new ArrayList<QualityCheck>();
		List<QualityCheckEntity> byparentidAndCode = qualityCheckDao.getByparentidAndCode(qualityCheck.getInnerData());
		if (byparentidAndCode.size() >0) {
			byparentidAndCode.forEach(item -> {
				QualityCheck qualityCheck2 = new QualityCheck();
				qualityCheck2.setInnerData(item);
				list.add(qualityCheck2);
			});
		}
		return list;
	}

// 通过ld查询数据的集合
	public List<QualityCheck> getListDataById(List<Long> ids){
		List<QualityCheck> list = new ArrayList<QualityCheck>();
		List<QualityCheckEntity> listDataById = qualityCheckDao.getListDataById(ids);
		if (listDataById.size() >0) {
			listDataById.forEach(item -> {
				QualityCheck qualityCheck = new QualityCheck();
				qualityCheck.setInnerData(item);
				list.add(qualityCheck);
			});
		}
		return list;
	}

	// 获取所有根节点数据
	public List<QualityCheck> getAllParentData() {
		List<QualityCheck> list = new ArrayList<QualityCheck>();
		List<QualityCheckEntity> allParentData = qualityCheckDao.getAllParentData();
		if (allParentData.size() >0) {
			allParentData.forEach(item -> {
				QualityCheck qualityCheck = new QualityCheck();
				qualityCheck.setInnerData(item);
				list.add(qualityCheck);
			});
		}
		return list;
		
	}
	
	// 查询当前id下所有的子类
	public List<QualityCheck> getChildByParentId(QualityCheck qualityCheck) {
		List<QualityCheck> list = new ArrayList<QualityCheck>();
		List<QualityCheckEntity> childByParentId = qualityCheckDao.getChildByParentId(qualityCheck.getInnerData());
		if (childByParentId.size()>0) {
			childByParentId.forEach(item -> {
				QualityCheck qualityCheck2 = new QualityCheck();
				qualityCheck2.setInnerData(item);
				list.add(qualityCheck2);
			});
		}
		return list;
	}
	@Override
	public ResultBean saveBatch(List<QualityCheck> obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultBean updateBatch(List<QualityCheck> obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultBean deleteBatch(List<QualityCheck> obj) {
		// TODO Auto-generated method stub
		return null;
	}



}
